package com.gitee.core.csv;

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhang_tao6 on 2017-11-23.
 */
public class CSVUtils {

    public static void main(String[] args) {
        List<BankInfo> bankInfos = readCSVToBean("D:/bankinfo.csv", BankInfo.class, "UTF-8");
        List<City> cities = readCSVToBean("D:/city.csv", City.class, "GBK");
        Map<String, City> cityMap = new HashMap<>();
        for (City city : cities) {
            cityMap.put(city.getCityCode(), city);
        }
        List<BankCSV> csvs = new ArrayList<>();

        for (BankInfo info : bankInfos) {
            BankCSV csv = new BankCSV();
            City city = cityMap.get(info.getCityCode());
            csv.setUnionBankNo(info.getUnionBankNo());
            if (city != null) {
                csv.setCityCode(city.getCityCode());
                csv.setCityName(city.getCityName());
            }
            csv.setBankType(info.getBankType());
            csv.setBankName(info.getBankName());
            csvs.add(csv);
        }
        writeBeanToCSV(csvs, "D:/bank.csv");

    }


    public static void writeCSV(List<String[]> data, String path) {
        try {
            CSVWriter csvWriter = new CSVWriter(new FileWriter(path), '\t');
            csvWriter.writeAll(data);
            csvWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static <T> void writeBeanToCSV(List<T> data, String path) {
        try {
            Writer writer = new FileWriter(path);
            StatefulBeanToCsv statefulBeanToCsv = new StatefulBeanToCsvBuilder(writer).withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
                    .withSeparator(CSVWriter.DEFAULT_SEPARATOR)
                    .withEscapechar('\\').build();
            statefulBeanToCsv.write(data);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<String[]> readCSV(String path) {
        List<String[]> data = null;
        try {
            CSVReader csvReader = new CSVReader(new FileReader(path));
            data = csvReader.readAll();
            csvReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public static <T> List<T> readCSVToBean(String path, Class<T> type, String charset) {
        List<T> data = null;
        try {
            InputStream in = new FileInputStream(path);
            Reader reader = new InputStreamReader(in, charset);
            data = new CsvToBeanBuilder(reader)
                    .withType(type).build().parse();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

}
